<?php

namespace App\Services\Activity;

use App\Models\Member\Like;
use App\Models\Activity\Activity;
use App\Models\Activity\Ticket;
use App\Models\Activity\Join;

use App\Services\Com\UtilService;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;

class ActivityService
{
    /***
     * member_uuid 用户uuid
     * is_free 1是免费 2是收费 
     * $query['show_member_uuid'] = $param['member_uuid'];
     * $query['join']  user只查用户圈的
     **/
    public static function feedList(int $page = 1, string $member_uuid = '', int $not_online_free = 0, 
        string $school_uuid = '', array $query = [])
    {

        // $cache_key = config('cache.key_list.act_in_feed_list').md5($page.$member_uuid.$not_online_free.$school_uuid.json_encode($query));
        // $cache_data = Cache::get($cache_key);
        // if(!empty($cache_data)){
        //     return $cache_data;
        // } 5483 5476
// DB::connection()->enableQueryLog();

        $where[] = ['activity.state', '!=', Activity::$keyToStateArr['stop_activity']];
        $where[] = ['activity.admin_state', '=', 1];
        $db_model = Activity::where($where);
        $db_model->select('activity.*');
        
        //关联用户posts
        if(isset($query['join']) && $query['join'] == 'user'){
            $db_model->join('posts_view_by_user', 'posts_view_by_user.service_uuid', '=', 'activity.uuid');
            $db_model->where('posts_view_by_user.service_type', '=', 'activity');
            $db_model->where('posts_view_by_user.member_uuid', '=', $query['show_member_uuid']);
        }
        //是否显示免费线上活动，个人圈子要显示
        if(!empty($not_online_free) && $not_online_free == 1){
            $db_model->whereRaw('(activity.is_free !=1 or activity.type !=1 )');
        }
        if(!empty($school_uuid)){
            $db_model->where('activity.school_uuid', '=', $school_uuid);
        }
        $res_arr = $db_model->forPage($page, 6)->orderBy('activity.id', 'desc')->get();
        $data = null;
// $log = DB::getQueryLog();
// var_dump([ $log]);
// die;
        foreach ($res_arr as $key => $value) {

            if(empty($value)){
                return null;
            }
            $res_arr = $value->toArray();
            $like_where = null;
            $like_where[] = ['member_uuid', '=', $member_uuid];
            $like_where[] = ['place', '=', 3];
            $like_where[] = ['service_uuid', '=', $res_arr['uuid']];
            $is_like = Like::where($like_where)->count();
            
            $like_where_all = null;
            $like_where_all[] = ['place', '=', 3];
            $like_where_all[] = ['service_uuid', '=', $res_arr['uuid']];
            $like_all = Like::where($like_where_all)->count();

            $join = null;
            $join[] = ['activity_uuid', '=', $res_arr['uuid']];
            $join_all = (int)Join::where($join)->count();

            $stock_where = null;
            $stock_where[] = ['activity_uuid', '=', $res_arr['uuid']];
            $stock_sum = (int)Ticket::where($stock_where)->sum('stock');

            $start_price = '';
            $start_price_where = [];
            $start_price_where[] = ['activity_uuid', '=', $res_arr['uuid']];
            $start_price_obj = Ticket::where($start_price_where)->orderby('price', 'asc')->first();
            if(!empty($start_price_obj)){
                $start_price_arr = $start_price_obj->toArray();
                $start_price = $start_price_arr['price'] ?? '0';
            }

            $data[$key]['id'] = $res_arr['id'];//活动id
            $data[$key]['uuid'] = $res_arr['uuid'];//活动uuid
            $data[$key]['name'] = $res_arr['name'];//活动名称
            $data[$key]['addr'] = $res_arr['address'];//活动地址
            $data[$key]['srart_time'] = $res_arr['start_time'];//开始时间
            $data[$key]['end_time'] = $res_arr['end_time'];//结束时间
            $data[$key]['price'] = $start_price;//起始价
            $data[$key]['has_like'] = $is_like > 0 ? 1: 0;//1是已经点赞0，是未点赞
            $data[$key]['sum_like'] = $like_all;//300点赞
            $data[$key]['image'] = $res_arr['head_img'];//主图
            $data[$key]['is_free'] = $res_arr['is_free'];//1是免费 2是收费
            $data[$key]['type'] = $res_arr['type'];//活动类别 1线上 2线下
            $data[$key]['apply_user_total'] = $join_all;//已报人数
            if($res_arr['is_free'] == 2){
                $data[$key]['max_user_total'] = $stock_sum;//收费的总人数 
            }else{
                $data[$key]['max_user_total'] = $res_arr['stock'] == 0 ? '不限' : $res_arr['stock'];//返回免费下的人数：不限、123
            }
        }
        // Cache::put($cache_key, $data, config('cache.ttl'));

        return $data ? $data : null;
    }

    public static function feedShow(string $uuid = '', string $member_uuid = ''){

        $data = null;

        $where[] = ['admin_state', '=', 1];
        $where[] = ['uuid', '=', $uuid];
        $value = Activity::where($where)->first();
        if(empty($value)){
            return null;
        }
        $res_arr = $value->toArray();
        $like_where = [];
        $like_where[] = ['member_uuid', '=', $member_uuid];
        $like_where[] = ['place', '=', 3];
        $like_where[] = ['service_uuid', '=', $res_arr['uuid']];
        $is_like = Like::where($like_where)->count();
        
        $like_where_all = [];
        $like_where_all[] = ['place', '=', 3];
        $like_where_all[] = ['service_uuid', '=', $res_arr['uuid']];
        $like_all = Like::where($like_where_all)->count();

        $join = [];
        $join[] = ['activity_uuid', '=', $res_arr['uuid']];
        $join_all = (int)Join::where($join)->count();

        $stock_where = [];
        $stock_where[] = ['activity_uuid', '=', $res_arr['uuid']];
        $stock_sum = (int)Ticket::where($stock_where)->sum('stock');

        $start_price = '';
        $start_price_where = [];
        $start_price_where[] = ['activity_uuid', '=', $res_arr['uuid']];
        $start_price_obj = Ticket::where($start_price_where)->orderby('price', 'asc')->first();
        if(!empty($start_price_obj)){
            $start_price_arr = $start_price_obj->toArray();
            $start_price = $start_price_arr['price'] ?? '0';
        }

        $data['id'] = $res_arr['id'];//活动id
        $data['uuid'] = $res_arr['uuid'];//活动uuid
        $data['name'] = $res_arr['name'];//活动名称
        $data['addr'] = $res_arr['address'];//活动地址
        $data['srart_time'] = $res_arr['start_time'];//开始时间
        $data['end_time'] = $res_arr['end_time'];//结束时间
        $data['price'] = $start_price;//起始价
        $data['has_like'] = $is_like > 0 ? 1: 0;//1是已经点赞0，是未点赞
        $data['sum_like'] = $like_all;//300点赞
        $data['image'] = $res_arr['head_img'];//主图
        $data['is_free'] = $res_arr['is_free'];//1是免费 2是收费
        $data['max_user_total'] = $stock_sum;//总人数 
        $data['apply_user_total'] = $join_all;//已报人数
        return $data;
    }

}